#ifndef __UIP_H__
#define __UIP_H__
#include <stdint.h>
#include <uip_lib.h>
#define UIP_DRIPADDR0   192
#define UIP_DRIPADDR1   168
#define UIP_DRIPADDR2   110
#define UIP_DRIPADDR3   111
#define UIP_BUFSIZE     420
#define UIP_LISTENPORTS 20
#define UIP_CONNS       20
#define UIP_ETHTYPE_ARP 0x0806
#define UIP_ETHTYPE_IP  0x0800
#define UIP_ETHTYPE_IP6 0x86dd
typedef uint32_t uip4addr_t[1];
typedef uint32_t uip6addr_t[4];
typedef struct  {
  uip4addr_t ripaddr;   /**< The IP address of the remote host. */
  
  uint16_t lport;        /**< The local TCP port, in network byte order. */
  uint16_t rport;        /**< The local remote TCP port, in network byteorder. */
  
  uint8_t rcv_nxt[4];    /**< The sequence number that we expect to receive next. */
  uint8_t snd_nxt[4];    /**< The sequence number that was last sent by
                         us. */
  uint16_t len;          /**< Length of the data that was previously sent. */
  uint16_t mss;          /**< Current maximum segment size for the connection. */
  uint16_t initialmss;   /**< Initial maximum segment size for the connection. */
  uint8_t sa;            /**< Retransmission time-out calculation state variable. */
  uint8_t sv;            /**< Retransmission time-out calculation state variable. */
  uint8_t rto;           /**< Retransmission time-out. */
  uint8_t tcpstateflags; /**< TCP state and flags. */
  uint8_t timer;         /**< The retransmission timer. */
  uint8_t nrtx;          /**< The number of retransmissions for the last segment sent. */

  /** The application state. */
  //uip_tcp_appstate_t appstate;
}uipconn_t;
#if 1
#include <stdio.h>
#include <unistd.h>
#define LOGD(fmt, ...) printf( "[DBG]"fmt, ...)
#define LOGE(fmt, ...) printf( "[ERR]"fmt, ...)
#define LOG_PATH() "./uiplog"
#define uip_loginit() do {\
    int fd = open(TAPLOG, O_RDWR|O_CREAT, 0666);\
    if(fd <= 0){\
        printf("[error] open log file error \n");\
        exit(-1);\
    }\
    dup2(1, fd);\
}while(0)
#else
#define LOGD(fmt, ...)
#define LOGE(fmt, ...)
#define uip_loginit() 
#endif 
#define uip_setip4addr(addr, addr0,addr1,addr2,addr3) do { \
    ((uint8_t *)(addr))[0] = (uint8_t)(addr0);\
    ((uint8_t *)(addr))[1] = (uint8_t)(addr1);\
    ((uint8_t *)(addr))[2] = (uint8_t)(addr2);\
    ((uint8_t *)(addr))[3] = (uint8_t)(addr3);\
} while(0)
void httpd_init(void);
void httpd_init(void);
void tftpd_init(void);
void telnetd_init(void);


void taplog_init(void)
{
    int fd = open(TAPLOG, O_RDWR|O_CREAT, 0666);
    if(fd <= 0){
        printf("[error] open log file error \n");
        exit(-1);
    }
    dup2(1, fd);
}

void uip_init(void)
{
    uip_ip4addr_t ip4addr;
    for(c = 0; c < UIP_LISTENPORTS; ++c) {
        uip_lport[c] = 0;
    }
    for(c = 0; c < UIP_CONNS; ++c) {
      //--> todo
    }
    UIP_SETIP(&uip_hostaddr, 192,168,110,2);
    UIP_SETIP(&uip_netmask,  255,255,266,0);

    httpd_init();
    tftpd_init();
    telnetd_init();
    return;
}

#endif __UIP_H__



